package be.rubengerits.android.car.dashboard;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnLayoutChangeListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import be.rubengerits.android.car.dashboard.notifications.OverlayObserver;
import be.rubengerits.android.car.dashboard.pager.HomeCollectionPagerAdapter;

public class MainActivity extends FragmentActivity {

	/* context access */
	private static Context mContext;

	private HomeCollectionPagerAdapter mPagerAdapter;

	private ViewPager mViewPager;

	private LinearLayout mNotifications;

	private View mNext, mPrevious;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MainActivity.mContext = this;

		setContentView(R.layout.activity_main);

		// view
		setupViewPager();

		// notifications
		setupNotifications();
	}

	private void setupNotifications() {
		this.mNotifications = (LinearLayout) findViewById(R.id.m_notifications);

		OverlayObserver.getInstance().setup(this.mNotifications);
	}

	private void setupViewPager() {
		this.mViewPager = (ViewPager) findViewById(R.id.m_view_pager);
		this.mPrevious = findViewById(R.id.m_prev);
		this.mNext = findViewById(R.id.m_next);

		final Animation animationIn = AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_in);
		final Animation animationOut = AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_out);

		this.mPagerAdapter = new HomeCollectionPagerAdapter(getSupportFragmentManager());
		this.mViewPager.setAdapter(this.mPagerAdapter);
		this.mViewPager.setCurrentItem(mPagerAdapter.getDefaultHome());
		this.mViewPager.addOnLayoutChangeListener(new OnLayoutChangeListener() {

			@Override
			public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
				if (mViewPager.getCurrentItem() == 0) {
					mPrevious.setAnimation(animationOut);
					mPrevious.setVisibility(View.GONE);
				} else if (mViewPager.getCurrentItem() == mPagerAdapter.getCount() - 1) {
					mNext.setAnimation(animationOut);
					mNext.setVisibility(View.GONE);
				} else {
					if (mPrevious.getVisibility() == View.GONE) {
						mPrevious.setAnimation(animationIn);
						mPrevious.setVisibility(Button.VISIBLE);
					}
					if (mNext.getVisibility() == View.GONE) {
						mNext.setAnimation(animationIn);
						mNext.setVisibility(Button.VISIBLE);
					}
				}

			}
		});
	}
	/*
	 * On Click Listeners
	 */
	public void nextPage(View v) {
		moveViewPager(1);
	}

	public void prevPage(View v) {
		moveViewPager(-1);
	}

	private void moveViewPager(int direction) {
		this.mViewPager.setCurrentItem(this.mViewPager.getCurrentItem() + direction);
	}

	public static Context getContext() {
		return mContext;
	}

}
